Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
postcss-loader
Advanced tools
The postcss-loader npm package is a loader for webpack that allows you to use PostCSS to process CSS with JavaScript. It enables the use of PostCSS plugins to perform various operations on CSS files, such as autoprefixing, minification, and custom transformations.
Autoprefixing
Automatically adds vendor prefixes to CSS rules using values from Can I Use. It is useful for supporting multiple browser versions.
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
require('autoprefixer')
]
}
}
}
]
}
]
}
};
CSS Minification
Optimizes and minifies CSS files to reduce file size and improve load times.
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
require('cssnano')()
]
}
}
}
]
}
]
}
};
Custom Transformations
Applies custom transformations or future CSS features using PostCSS plugins.
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
require('postcss-custom-properties')()
]
}
}
}
]
}
]
}
};
The sass-loader compiles Sass/SCSS files to CSS. It requires Node.js-style .sass/.scss files. Unlike postcss-loader, it's specifically designed for Sass pre-processing.
The less-loader processes .less files and compiles them to CSS. It's similar to postcss-loader in that it transforms styles, but it's tailored for the Less pre-processor.
This package is a webpack loader that compiles Stylus files to CSS. It's a pre-processor loader like sass-loader and less-loader, but for Stylus syntax.
PostCSS loader for webpack to postprocesses your CSS with PostCSS plugins.
npm install postcss-loader --save-dev
Create postcss.config.js
:
module.exports = {
plugins: [
require('postcss-smart-import')({ /* ...options */ }),
require('precss')({ /* ...options */ }),
require('autoprefixer')({ /* ...options */ })
]
}
You could put different configs in different directories. For example,
global config in project/postcss.config.js
and override its plugins
in project/src/legacy/postcss.config.js
.
You can read more about common PostCSS config in postcss-load-config.
Add PostCSS Loader to webpack.config.js
. Put it after css-loader
and style-loader
. But before sass-loader
, if you use it.
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader?importLoaders=1',
'postcss-loader'
]
}
]
}
}
module.exports = {
module: {
loaders: [
{
test: /\.css$/,
loaders: [
'style-loader',
'css-loader?importLoaders=1',
'postcss-loader'
]
}
]
}
}
We recommend to use postcss.config.js
, but also you can specify plugins
directly in webpack config.
module.exports = {
module: {
rules: [
{
test: /\.css/,
use: [
…
{
loader: 'postcss-loader',
options: {
plugins: function () {
return [
require('precss'),
require('autoprefixer')
];
}
}
}
]
}
]
}
}
module.exports = {
module: {
loaders: [
{
test: /\.css$/,
loaders: [
…
'postcss-loader'
]
}
]
},
postcss: () => {
return [
require('precss'),
require('autoprefixer')
];
}
}
PostCSS can transforms styles in any syntax, not only in CSS. There are 3 parameters to control syntax:
syntax
accepts module name with parse
and stringify
function.parser
accepts module name with input parser function.stringifier
accepts module name with output stringifier function.module.exports = {
module: {
loaders: [
{
test: /\.sss/,
loaders: [
'style-loader',
'css-loader?importLoaders=1',
'postcss-loader?parser=sugarss'
]
}
]
}
}
Loader will use source map settings from previous loader.
You can set this sourceMap
parameter to inline
value to put source maps
into CSS annotation comment:
module.exports = {
module: {
loaders: [
{
test: '\/.css',
loaders: [
'style-loader',
'css-loader?importLoaders=1',
'postcss-loader?sourceMap=inline'
]
}
]
}
}
This loader cannot be used with CSS Modules out of the box due
to the way css-loader
processes file imports. To make them work properly,
either add the css-loader’s importLoaders
option
…
{
test: /\.css$/,
loaders: [
'style-loader',
'css-loader?modules&importLoaders=1',
'postcss-loader'
]
}
…
or use postcss-modules plugin instead of css-loader
.
If you want to process styles written in JavaScript you can use the postcss-js parser.
…
{
test: /\.style.js$/,
loaders: [
'style-loader',
'css-loader?modules&importLoaders=1',
'postcss-loader?parser=postcss-js',
'babel'
]
}
…
As result you will be able to write styles as:
import colors from './config/colors'
export default {
'.menu': {
color: colors.main,
height: 25,
'&_link': {
color: 'white'
}
}
}
If you are using Babel >= v6 you need to do the following in order for the setup to work
- Add babel-plugin-add-module-exports to your configuration
- You need to have only one default export per style module
If you use JS styles without postcss-js
parser, you can add exec
parameter:
…
{
test: /\.style.xyz$/,
loaders: [
'style-loader',
'css-loader?modules&importLoaders=1',
'postcss-loader?parser=custom-parser&exec'
]
}
…
PostCSS loader sends a loaded instance to PostCSS common config. You can use it to do some real magic:
module.exports = function (ctx) {
if (check(ctx.webpack.resourcePath)) {
return { plugins: plugins1 };
} else {
return { plugins: plugins2 };
}
}
Webpack provides webpack plugin developers a convenient way to hook into the build pipeline. The postcss-loader makes use of this event system to allow building integrated postcss-webpack tools.
See the example implementation.
Event postcss-loader-before-processing
is fired before processing and allows
to add or remove postcss plugins.
FAQs
PostCSS loader for webpack
We found that postcss-loader demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.